
#import "YQRealNameViewController.h"

#import "YQRealNameModel.h"

#import "BRStringPickerView.h"

@interface YQRealNameViewController ()

@property (strong, nonatomic) YQRealNameModel *model;

@property (assign, nonatomic) NSInteger type;

@end

@implementation YQRealNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isNavShadow = YES;
    self.navigationItem.title = kYQLS(@"实名认证");
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);

    kYQREGISTERTABLEVIEWNIBCELL(@"MYRealNameTextCell");
    kYQREGISTERTABLEVIEWNIBCELL(@"MYLicenseTypeTextCell");
    kYQREGISTERTABLEVIEWNIBCELL(@"MYRealNameSubmitCell");

}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
//    YQRealNameModel *model = self.dataArr[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    if (indexPath.row == 0) {
        MYRealNameTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYRealNameTextCell" ];

        cell.titleLb.text = kYQLS(@"姓名");
        cell.titleLb.font = kSystemMedium(15);
        cell.textField.placeholder = kYQLS(@"请输入真实姓名");
        return cell;

    }else if (indexPath.row == 2) {
        MYRealNameTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYRealNameTextCell" ];

        cell.titleLb.text = kYQLS(@"证件号码");
        cell.titleLb.font = kSystemMedium(15);
        cell.textField.placeholder = kYQLS(@"请输入您的证件号");
        return cell;

    }else if (indexPath.row == 3) {
        MYRealNameSubmitCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYRealNameSubmitCell" ];
        [cell.submitBtn addTapAction:^(id  _Nonnull sender) {
            [self submitRealNameInfo];
        }];
        return cell;

    }else if (indexPath.row == 1) {
        MYLicenseTypeTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYLicenseTypeTextCell"];
        cell.textField.userInteractionEnabled = NO;
        [cell.licenseBtn setTitle:kYQLS(@"证件类型") forState:UIControlStateNormal];
        [cell.licenseBtn setTitleColor:HexColor(0x414141) forState:UIControlStateNormal];
        cell.licenseBtn.titleLabel.font = kSystemMedium(15);
        cell.textField.placeholder = kYQLS(@"请选择");
        [cell.licenseBtn addTapAction:^(id  _Nonnull sender) {
            NSLog(@"类型");
//            ws(self)
            NSArray *licenseArr = @[kYQLS(@"驾驶证"),kYQLS(@"港澳通行证"),kYQLS(@"身份证"),kYQLS(@"护照"),kYQLS(@"其他证件")];
            [BRStringPickerView showStringPickerWithTitle:@"" dataSource:licenseArr.copy defaultSelValue:kYQLS(@"身份证") isAutoSelect:NO themeColor:kAPPThemeColor resultBlock:^(id selectValue) {
                self.type = [licenseArr indexOfObject:selectValue];
                [cell.textField setText:selectValue];
            } cancelBlock:^{
                
            }];
        }];
        return cell;
    }
    return cell;
}

#pragma mark - 提交实名信息
- (void)submitRealNameInfo
{
    MYRealNameTextCell *nameCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    if (nameCell.textField.text.length == 0) {
        kMBMesHUD(kYQLS(@"请输入真实姓名"))
        return;
    }
    
    MYRealNameTextCell *IdCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    if (IdCell.textField.text.length == 0) {
        kMBMesHUD(kYQLS(@"请选择证件类型"))
        return;
    }
    
    MYRealNameTextCell *idNumCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    if (idNumCell.textField.text.length == 0) {
        kMBMesHUD(kYQLS(@"请输入证件号"))
        return;
    }

    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"authName"] = nameCell.textField.text?:@"";
    parameters[@"authType"] = @(self.type);
    parameters[@"authNumber"] = idNumCell.textField.text?:@"";
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendPostRequest:kPOST_AUTH_SUBMIT_URL parameters:parameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        if (responseObject.code == 0) {
            kMBMesHUD(kYQLS(@"提交成功"));
          [self.navigationController popViewControllerAnimated:YES];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 3) {
        return 267;
    }
    return 58;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

@end
